/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.systemconfig.entity.ProjectBaseEntity;
import io.renren.modules.systemconfig.service.ProjectBaseService;
import io.renren.modules.systemconfig.service.SysConfigServerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/project")
public class ProjectBaseController extends AbstractController {
	@Autowired
	private ProjectBaseService projectBaseService;


	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("systemconfig:servers:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = projectBaseService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		return R.ok().put("server", projectBaseService.getById(id));
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@PostMapping("/save")
	@RequiresPermissions("systemconfig:servers:save")
	public R save(@RequestBody ProjectBaseEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);
		entity.setCreateTime(DateUtils.formatDefault(new Date()));
		projectBaseService.saveEntity(entity);
		
		return R.ok();
	}


	@GetMapping("/all")
	@RequiresPermissions("project:list")
	public R listAll(@RequestParam Map<String, Object> params){
		List list = projectBaseService.list();

		return R.ok().put("data", list);
	}


	@GetMapping("/treelist")
	@RequiresPermissions("project:list")
	public R treeList(@RequestParam Map<String, Object> params){
		PageUtils page = projectBaseService.treeList(params);

		return R.ok().put("page", page);
	}

	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@PostMapping("/update")
	@RequiresPermissions("systemconfig:servers:update")
	public R update(@RequestBody ProjectBaseEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);
		projectBaseService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@PostMapping("/delete")
	@RequiresPermissions("systemconfig:servers:delete")
	public R delete(@RequestBody Long[] delIds){
		projectBaseService.deleteBatch(delIds);
		return R.ok();
	}
}
